MainLayer = cc.Layer.extend({
	Layer: 0,
	ButtonWidth: 1 / 2,
	ButtonHeight: 1 / 7,
	StartX: 0, // w/2
	StartY: 0, // h/2
	Margin: 10,
	FullScreened: false,
	AllListeners: [],
	Buttons: [],
	newButton: function (a,s) {
		let Button = cc.Sprite.create(a);
		//ResizeTo(Button, this.ButtonWidth * C.GameWidth, this.ButtonHeight * C.GameHeight);
		Button.setScale(this.ButtonHeight * C.GameHeight / Button.height);
		Button.setPosition(this.StartX, this.StartY);
		/*let Text = cc.LabelTTF.create(s, "Arial", 45);
		Text.setPosition(this.StartX, this.StartY);
		Text.setColor(0, 0, 0);*/
		this.addChild(Button);
		// this.addChild(Text);
		this.StartY -= (this.Margin + this.ButtonHeight * C.GameHeight);
		this.Buttons.push(Button);
		cc.eventManager.addListener(cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: true,
			E: document.documentElement,
			onTouchBegan: function (touch) {
				if (!cc.rectContainsPoint(Button.getBoundingBox(), touch.getLocation())) return false;
				s();
				return true;
			}
		}), Button);
		return Button;
	},
	onEnter: function () {
		this._super();
		cc.eventManager.removeAllListeners();
		this.size = cc.director.getWinSize();
		C.GameWidth = this.size.width;
		C.GameHeight = this.size.height;
		this.StartX = C.GameWidth / 2;
		this.StartY = C.GameHeight / 2 + this.ButtonHeight * C.GameHeight / 2;
		delete size;
		this.addChild(createBackground());
		let Title = cc.Sprite.create(R.Title);
		let Base = C.GameWidth / 3 / Title.width;
		Title.setScale(Base);
		Title.setPosition(C.GameWidth / 2, C.GameHeight / 7 * 6);
		Title.runAction(cc.sequence(cc.scaleTo(1, Base * 3 / 2.5), cc.scaleTo(1, Base)).repeatForever());
		this.addChild(Title);
		this.newButton(R.Button.NewGame, function () {
			CookieHelper.remove("level");
			cc.director.runScene(cc.TransitionSlideInB.create(1, new GameScene(), false));
		});
		this.newButton(R.Button.Continue, function () {
			cc.director.runScene(cc.TransitionSlideInB.create(1, new GameScene(), false));
		});
		let FullScreen = cc.Sprite.create(R.Button.FullScreen);
		FullScreen.setPosition(FullScreen.width / 2 + 10, C.GameHeight - FullScreen.height / 2 - 10);
		cc.eventManager.addListener(cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: true,
			E: document.documentElement,
			onTouchBegan: function (touch) {
				if (!cc.rectContainsPoint(FullScreen.getBoundingBox(), touch.getLocation())) return false;
				if (cc.screen.fullScreen()) {
					console.log("Exit");
					cc.screen.exitFullScreen();
					window.onresize(null);
				} else {
					cc.screen.requestFullScreen(this.E, function () {
						console.log("Window Resize");
						window.onresize(null);
					});
				}
				Layer.FullScreened = !Layer.FullScreened;
				return true;
			}
		}), FullScreen);
		FullScreen.onResize = function () {
			this.setPosition(this.width / 2 + 10, C.GameHeight - this.height / 2 - 10);
		};
		this.addChild(FullScreen);
		window.Layer = this;
	},
	onResize: function () {
		this.removeAllChildren();
		this.onEnter();
	}
});
MainScene = cc.Scene.extend({
	onEnter: function () {
		this._super();
		this.addChild(new MainLayer(), 0);
	}
});